{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4fa9aa89",
   "metadata": {},
   "outputs": [],
   "source": [
    "#方案1：将序列转为字典{元素：频度}。根据字典中的值排序\n",
    "#方案2：使用标准库collections中的Counter对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b4c15011",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9,\n",
       " 7,\n",
       " 5,\n",
       " 6,\n",
       " 7,\n",
       " 1,\n",
       " 4,\n",
       " 1,\n",
       " 5,\n",
       " 5,\n",
       " 6,\n",
       " 1,\n",
       " 5,\n",
       " 7,\n",
       " 9,\n",
       " 10,\n",
       " 5,\n",
       " 1,\n",
       " 6,\n",
       " 2,\n",
       " 1,\n",
       " 4,\n",
       " 10,\n",
       " 4,\n",
       " 7,\n",
       " 3,\n",
       " 6,\n",
       " 4,\n",
       " 4,\n",
       " 3]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#含有重复元素的列表：\n",
    "from random import randint\n",
    "l = [randint(1,10)for _ in range(30)]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e9006081",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{9: 0, 7: 0, 5: 0, 6: 0, 1: 0, 4: 0, 10: 0, 2: 0, 3: 0}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方案1：将序列转为字典{元素：频度}。根据字典中的值排序\n",
    "d =dict.fromkeys(l,0)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "32d74d05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{9: 2, 7: 4, 5: 5, 6: 4, 1: 5, 4: 5, 10: 2, 2: 1, 3: 2}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in l:\n",
    "    d[i] +=1\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "360c0ede",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(5, 5), (5, 4), (5, 1)]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取频次前三\n",
    "sorted(list(zip(d.values(),d.keys())),reverse=True)[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "29777bf0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(5, 5), (5, 4), (5, 1)]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用heapq取前三\n",
    "import heapq\n",
    "heapq.nlargest(3,list(zip(d.values(),d.keys())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "82acdec9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({9: 2, 7: 4, 5: 5, 6: 4, 1: 5, 4: 5, 10: 2, 2: 1, 3: 2})"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方案2：使用标准库collections中的Counter对象\n",
    "from collections import Counter\n",
    "Counter(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "0fdf5bd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(5, 5), (1, 5), (4, 5)]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取前三\n",
    "Counter(l).most_common(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "b450eb44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('you', 32),\n",
       " ('to', 19),\n",
       " ('who', 10),\n",
       " ('those', 9),\n",
       " ('the', 8),\n",
       " ('and', 7),\n",
       " ('have', 7),\n",
       " ('that', 6),\n",
       " ('want', 6),\n",
       " ('make', 6)]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#统计文章中的词频\n",
    "import re\n",
    "f = open('./article.txt','r',encoding='utf8')\n",
    "words = re.split('\\W+',f.read())\n",
    "Counter(words).most_common(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee09c0ad",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
